package Concurrency2.Chapter09_CallBackPattern;

import java.util.concurrent.TimeUnit;

/**
 * Instruction：异步阻塞的执行方式
 * 问题分析:
 * 1, join方式只能合并线程, 不能获取被合并线程的返回值;
 * 2, join底层是靠不停检查join线程是否存活实现的;
 * 3, join几乎没有被使用的机会, 因为任务都是使用线程池管理.
 * Author：@author MaLi
 */
public class CallBack_Join {
    //烧水任务
    public static class HotWater implements Runnable {

        @Override
        public void run() {
            System.out.println("洗水壶");
            System.out.println("灌水");
            System.out.println("开火");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("水烧开了");
        }
    }

    //清洗任务
    public static class WashGlasses implements Runnable {

        @Override
        public void run() {
            System.out.println("洗茶壶");
            System.out.println("洗茶杯");
            System.out.println("拿茶叶");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("清洗完成");
        }
    }

    //main线程
    public static void main(String[] args) {
        //创建两类子线程
        Thread hThread = new Thread(new HotWater(), "烧水线程");
        Thread wThread = new Thread(new WashGlasses(), "清洗线程");
        hThread.start();
        wThread.start();
        //异步阻塞
        try {
            hThread.join();
            wThread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("主线程继续执行: 泡茶喝...");
    }
}
